﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using YSC_Core.BLL.Domain.BackstageModels;
using YSC_OpenPlatform_API.Entity;
using YSC_Services.BLL.Services.Interfaces;

namespace YSC_Backstage_Web.Controllers
{
    /// <summary>
    /// 市场单位
    /// </summary>
    public class MarketUnitController : Controller
    {
        private readonly IBasisBackstageServices _pro;
        public MarketUnitController(IBasisBackstageServices pro)
        {
            _pro = pro;
        }
        //MarketUnit
        //市场单位
        public IActionResult Index()
        {
            return View();
        }

        public JsonResult GetList(int limit, int page, string keyword)
        {
            var aa = _pro.GetMarketUnitAll(page, limit, keyword);
            return Json(aa);
        }


        public IActionResult Add()
        {
            return View();
        }

        public IActionResult Edit(string Id)
        {
            var aa = _pro.GetMarketUnit(Id);
            var json = JsonConvert.SerializeObject(aa.Result);
            JObject obj = JObject.Parse(json);
            MarketUnit entity = JsonConvert.DeserializeObject<MarketUnit>(obj["Data"].ToString());//将字符串转回成实体
            return View(entity);
        }

        public JsonResult AddEditSubmit(string paramete, string county, string city, string province)
        {
            MarketUnitModels entity = JsonConvert.DeserializeObject<MarketUnitModels>(paramete);
            entity.AreaUnCode = county != null ? county : city != null ? city : province;
            var result = _pro.AddUpdateMarketUnit(entity);
            return Json(result);
        }
    }
}